"""
Adam Prejean
Date Created: 8/21/2014
ap_assignControlShaders.py

How to run:
import ap_assignControlShaders
reload (ap_assignControlShaders)
ap_assignControlShaders.ActivateUI()

"""
import maya.cmds as cmds

def ActivateUI():
	# Window Creation =================================================================================================

	if cmds.window( "ShadersWindow", exists=True, q=True ):
		cmds.deleteUI( "ShadersWindow", window=True )
	#if (cmds.windowPref("ShadersWindow", ex=True)):
	#    cmds.windowPref("ShadersWindow", r=True)
		
	window = cmds.window( "ShadersWindow", title = "Control Shaders", sizeable=False, mnb=False, mxb=False, toolbox=True)
	form = cmds.formLayout(numberOfDivisions=100)

	# Button Creation =================================================================================================

	# Green
	
	object = cmds.button( command=GreenMat, backgroundColor=(0,1,0), label=" ", w=30, h=30)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 10), ( object, 'left', 10)] )
	#=========================================
	

	# Yellow
	object = cmds.button( command=YellowMat, backgroundColor=(1,1,0), label=" ", w=30, h=30)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 10), ( object, 'left', 50)] )
	#=========================================	
	
	
	# Blue
	object = cmds.button( command=BlueMat, backgroundColor=(0,0,1), label=" ", w=30, h=30)
	cmds.formLayout( form, edit=True, attachForm=[( object, 'top', 10), ( object, 'left', 90)] )
	#=========================================

	

	cmds.setParent( '..' )

	cmds.window( "ShadersWindow", edit=True, widthHeight=(130, 50)) #For 1 Row Height= 45, For 2 Rows Height= 85,For 3 Rows Height=128

	cmds.showWindow( window )


def Change_Attributes():
    selection = cmds.ls(selection=True)
    for eachObject in selection:
        cmds.setAttr( eachObject + ".castsShadows", 0)
        cmds.setAttr( eachObject + ".receiveShadows", 0)
        cmds.setAttr( eachObject + ".motionBlur", 0)
        cmds.setAttr( eachObject + ".primaryVisibility", 0)
        cmds.setAttr( eachObject + ".visibleInReflections", 0)
        cmds.setAttr( eachObject + ".visibleInRefractions", 0)
        print "NOTE: Selection's Render Stats have been changed!"


def GreenMat(self):
	#Change Render Stats
	Change_Attributes()
	#Create a list of selected objects
	selection = cmds.ls(selection=True)
	#Incrementally Increase
	increment = 1
	if increment != None:
		number = "0" + str(increment)
		increment += 1
	#create new material
	mat = cmds.shadingNode("lambert", asShader=True, name = "controlSurface" + number)
	cmds.setAttr(mat + ".color", 0,1,.05)
	cmds.setAttr(mat + ".transparency", 0.8,0.8,0.8)
	cmds.setAttr(mat + ".ambientColor", 0,0,0)
	cmds.setAttr(mat + ".diffuse", 1)
	cmds.setAttr(mat + ".translucence", 0)
	#creates the empty shading group with the proper name
	cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name = mat + "Shader")
	#Assigns the shader to the shading group  
	cmds.connectAttr(mat + ".outColor", mat + "Shader" + ".surfaceShader", force=False, nextAvailable=False)
	#For each element in that list, assign it to the Shading Group set
	for eachObject in selection:
		cmds.sets( eachObject, edit=True, forceElement= mat + "Shader")
		print "NOTE: Selection's Material has been changed!"


def YellowMat(self):
	#Change Render Stats
	Change_Attributes()
	#Create a list of selected objects
	selection = cmds.ls(selection=True)
	#Incrementally Increase
	increment = 1
	if increment != None:
		number = "0" + str(increment)
		increment += 1
	#create new material
	mat = cmds.shadingNode("lambert", asShader=True, name = "controlSurface" + number)
	cmds.setAttr(mat + ".color", 1,1,0)
	cmds.setAttr(mat + ".transparency", 0.8,0.8,0.8)
	cmds.setAttr(mat + ".ambientColor", 0,0,0)
	cmds.setAttr(mat + ".diffuse", 1)
	cmds.setAttr(mat + ".translucence", 0)
	#creates the empty shading group with the proper name
	cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name = mat + "Shader")
	#Assigns the shader to the shading group  
	cmds.connectAttr(mat + ".outColor", mat + "Shader" + ".surfaceShader", force=False, nextAvailable=False)
	#For each element in that list, assign it to the Shading Group set
	for eachObject in selection:
		cmds.sets( eachObject, edit=True, forceElement= mat + "Shader")
		print "NOTE: Selection's Material has been changed!"


def BlueMat(self):
	#Change Render Stats
	Change_Attributes()
	#Create a list of selected objects
	selection = cmds.ls(selection=True)
	#Incrementally Increase
	increment = 1
	if increment != None:
		number = "0" + str(increment)
		increment += 1
	#create new material
	mat = cmds.shadingNode("lambert", asShader=True, name = "controlSurface" + number)
	cmds.setAttr(mat + ".color", 0,0,1)
	cmds.setAttr(mat + ".transparency", 0.8,0.8,0.8)
	cmds.setAttr(mat + ".ambientColor", 0,0,0)
	cmds.setAttr(mat + ".diffuse", 1)
	cmds.setAttr(mat + ".translucence", 0)
	#creates the empty shading group with the proper name
	cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name = mat + "Shader")
	#Assigns the shader to the shading group  
	cmds.connectAttr(mat + ".outColor", mat + "Shader" + ".surfaceShader", force=False, nextAvailable=False)
	#For each element in that list, assign it to the Shading Group set
	for eachObject in selection:
		cmds.sets( eachObject, edit=True, forceElement= mat + "Shader")
		print "NOTE: Selection's Material has been changed!"


'''
def GreenSurf():
	#Change Render Stats
	Change_Attributes()
	#Create a list of selected objects
	selection = cmds.ls(selection=True)
	#Incrementally Increase
	increment = 1
	if increment != None:
		number = "0" + str(increment)
		increment += 1
	#create new material
	mat = cmds.shadingNode("surfaceShader", asShader=True, name = "controlSurface" + number)
	cmds.setAttr(mat + ".outColor", 0,1,.05)
	cmds.setAttr(mat + ".outTransparency", .75,.75,.75)	
	#creates the empty shading group with the proper name
	cmds.sets(renderable=True, noSurfaceShader=True, empty=True, name = mat + "Shader")
	#Assigns the shader to the shading group  
	cmds.connectAttr(mat + ".outColor", mat + "Shader" + ".surfaceShader", force=False, nextAvailable=False)
	#For each element in that list, assign it to the Shading Group set
	for eachObject in selection:
		cmds.sets( eachObject, edit=True, forceElement= mat + "Shader")
		print "NOTE: Selection's Material has been changed!"
'''

'''
cmds.select(cmds.ls(dagObjects=1, allPaths=1, selection=1, type="dagNode")) # Selects All objects of selected dagNodes
cmds.ls(dagObjects=1, allPaths=1, selection=1, type="dagNode") # Lists All objects of selected dagNodes
'''